package com.wallpaper.weatherwallpaper;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class SetWallPaperActivity extends Activity {
	GridView wallPapersSettedGridview;
	WallPaperAdapter wallPaperAdapter;
	
	//定义SharedPreferences文件中的键
	private final String SunnyDayInt = "SunnyDayInt";
	private final String SunnyDayStr="SunnyDayStr";
	private final String SunnyNightInt="SunnyNightInt";
	private final String SunnyNightStr="SunnyNightStr";
	private final String RainyDayInt = "RainyDayInt";
	private final String RainyDayStr="RainyDayStr";
	private final String RainyNightInt="RainyNightInt";
	private final String RainyNightStr="RainyNightStr";
	private final String CloudyDayInt = "CloudyDayInt";
	private final String CloudyDayStr="CloudyDayStr";
	private final String CloudyNightInt="CloudyNightInt";
	private final String CloudyNightStr="CloudyNightStr";
	private final String SnowyDayInt = "SnowyDayInt";
	private final String SnowyDayStr="SnowyDayStr";
	private final String SnowyNightInt="SnowyNightInt";
	private final String SnowyNightStr="SnowyNightStr";
	private final String CalmDayInt = "CalmDayInt";
	private final String CalmDayStr="CalmDayStr";
	private final String CalmNightInt="CalmNightInt";
	private final String CalmNightStr="CalmNightStr";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_wall_paper);
        
        wallPapersSettedGridview=(GridView)findViewById(R.id.WallPapersSettedGridview);
        wallPaperAdapter=new WallPaperAdapter(SetWallPaperActivity.this,getWeathers(),getTimes(),getSystemwallpapers(),getUserwallpapers());
        wallPapersSettedGridview.setAdapter(wallPaperAdapter);
        wallPapersSettedGridview.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.setClass(SetWallPaperActivity.this,MainActivity.class);
				Bundle bundle=new Bundle();
				bundle.putString("Weather", wallPaperAdapter.getWeathers()[position]);
				bundle.putString("Time", wallPaperAdapter.getTimes()[position]);
				intent.putExtras(bundle);
				startActivity(intent);
				//SetWallPaperActivity.this.finish();
			}
        	
        });
    }

    private String[] getUserwallpapers() {
		// TODO Auto-generated method stub
    	SharedPreferences userPreferences = getSharedPreferences("WeatherWP",Context.MODE_PRIVATE);
    	String[] userwallpapers=new String[]{
    			userPreferences.getString(SunnyDayStr, null),
    			userPreferences.getString(SunnyNightStr, null),
    			userPreferences.getString(RainyDayStr, null),
    			userPreferences.getString(RainyNightStr, null),
    			userPreferences.getString(CloudyDayStr, null),
    			userPreferences.getString(CloudyNightStr, null),
    			userPreferences.getString(SnowyDayStr, null),
    			userPreferences.getString(SnowyNightStr, null),
    			userPreferences.getString(CalmDayStr, null),
    			userPreferences.getString(CalmNightStr, null),
    	};
		return userwallpapers;
	}

	private Integer[] getSystemwallpapers() {
		// TODO Auto-generated method stub
		SharedPreferences userPreferences = getSharedPreferences("WeatherWP",Context.MODE_PRIVATE);
		Integer[] systemwallpapers=new Integer[]{
				userPreferences.getInt(SunnyDayInt, 0),
				userPreferences.getInt(SunnyNightInt, 0),
				userPreferences.getInt(RainyDayInt, 0),
				userPreferences.getInt(RainyNightInt, 0),
				userPreferences.getInt(CloudyDayInt, 0),
				userPreferences.getInt(CloudyNightInt, 0),
				userPreferences.getInt(SnowyDayInt, 0),
				userPreferences.getInt(SnowyNightInt, 0),
				userPreferences.getInt(CalmDayInt, 0),
				userPreferences.getInt(CalmNightInt, 0),
		};
		return systemwallpapers;
	}

	//后期优化不是写死的字符串
	private String[] getTimes() {
		// TODO Auto-generated method stub
		String[] times=new String[]{"白天","夜晚","白天","夜晚","白天","夜晚","白天","夜晚","白天","夜晚",};
		return times;
	}

	//后期优化不是写死的字符串
	private String[] getWeathers() {
		// TODO Auto-generated method stub
		String[] weathers=new String[]{"晴天","晴天","雨","雨","阴天","阴天","雪","雪","风平浪静","风平浪静",};
		return weathers;
	}
}
